package com.duoduo.demo.springnoxml.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.duoduo.demo.springnoxml.model.HelpKeyword;

/**
 * 帮助关键字Dao处理类
 * @author chengesheng@kedacom.com
 * @date 2017年10月23日下午3:08:55
 */
@Repository
public class HelpKeywordDao extends BaseDao {

	public static final RowMapper<HelpKeyword> entityRowMapper = new RowMapper<HelpKeyword>() {

		@Override
		public HelpKeyword mapRow(ResultSet rs, int rowNum) throws SQLException {
			HelpKeyword entity = new HelpKeyword();
			entity.setId(rs.getInt("help_keyword_id"));
			entity.setName(rs.getString("name"));
			return entity;
		}
	};

	/**
	 * 获取所有帮助关键字
	 */
	private static final String LIST_ALL = "select * from help_keyword";

	public List<HelpKeyword> listAll() {
		logger.debug("listAll {}", LIST_ALL);
		return super.getJdbcTemplate().query(LIST_ALL, entityRowMapper);
	}

	/**
	 * 获取指定主题的关键字
	 */
	private static final String LIST_BY_TOPIC = "select * from help_keyword k "
			+ " left join help_relation r on r.help_keyword_id=k.help_keyword_id left join help_topic t on t.help_topic_id = r.help_topic_id "
			+ " where t.help_topic_id=?";

	public List<HelpKeyword> listByTopic(int topicId) {
		logger.debug("listByTopic {}", LIST_BY_TOPIC);
		return super.getJdbcTemplate().query(LIST_BY_TOPIC, entityRowMapper, topicId);
	}
}
